#include <iostream>
#include <vector>
#include <unordered_map>

struct UserInfo
{
    int _id;  
    int _port;
    std::string _ip;
};

class ProtoL
{
public:
    void AddUser(int id,int port,std::string ip)
    {
        User_list[id]={id,port,ip};
    }
    void DelUser(int id)
    {
        for(auto i : User_list)
        {
            if(i.first == id)
            {
                User_list.erase(i.first);
            }
        }
    }

    std::string Selizer(int id,std::string msg)
    {
        std::string smsg=std::to_string(id)+"#"+msg+"\t";
        return smsg;
    }

    void Deselizer(std::string msg,int& id,std::string& msg_body)
    {
        char gap='#';
        std::vector<std::string> tmp;
        int pre=0;
        for(int i=0;i<msg.size();i++)
        {
            if(msg[i]==gap)
            {
                tmp.push_back(msg.substr(pre,i-pre));
                pre=i+1;
            }
            if(msg[i]=='\t')
            {
                msg_body=msg.substr(pre,i-pre);
            }
        }
        id=atoi(tmp[0].c_str());
    }

    bool find(int id)
    {
        for(auto i : User_list)
        {
            if(i.first == id)
            {
                return true;
            }
        }
        return false;
    }
    std::string get_uselistid()
    {
        std::string msg;
        for(auto i : User_list)
        {
            msg+=std::to_string(i.first)+" ";
        }
        return msg;
    }

    bool judge_addfrend(char* buff)
    {
        return buff[0]=='@';   
    }

    std::string selizer_addfriend(int id)
    {
        return "@"+std::to_string(id);
    }

    bool judge_agreefrend(char* buff)
    {
        return buff[0]=='$';   
    }

    std::string selizer_agreefriend(int id)
    {
        return "$"+std::to_string(id);
    }

    std::unordered_map<int,UserInfo> get_userlist()
    {
        return User_list;
    }
private:
    std::unordered_map<int,UserInfo> User_list;
};